package com.example.zkf.zyftpt_415zkf.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkf.zyftpt_415zkf.R;
import com.example.zkf.zyftpt_415zkf.adapter.MyAdapterTcase;
import com.example.zkf.zyftpt_415zkf.bean.TcaseBean;
import com.example.zkf.zyftpt_415zkf.iface.TcaseListener;
import com.example.zkf.zyftpt_415zkf.model.TcaseModel;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment4 extends Fragment {
    private TextView tvinfo;
    private View view = null;
    private Context context;

    //1copy by mainactivity
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    // private MyAdapter mAdapter;
    private MyAdapterTcase myAdapterTcase;
    private List<TcaseBean> list = null;

    private TcaseListener articleListener = new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> tcaseBeen) {
            list = tcaseBeen;
            // mAdapter.setList(list);
            myAdapterTcase.setList(list);
            //数据更新的话
            // mAdapter.notifyDataSetChanged();
            myAdapterTcase.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
    };

    //2 copy by mainacivity
    private void initRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView4);//!
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(context);//!
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        // list = getData();
//        mAdapter = new MyAdapter(this);
//        mAdapter.setList(list);
//        recyclerView.setAdapter(mAdapter);
        myAdapterTcase = new MyAdapterTcase(context);//!
        myAdapterTcase.setList(list);
        recyclerView.setAdapter(myAdapterTcase);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment1的布局文件
        view = inflater.inflate(R.layout.fragment4, container, false);
        //获取布局上的控件，注意通过view获取
       // tvinfo = (TextView) view.findViewById(R.id.textView8);
       // tvinfo.setText("设置");
        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment设置", Toast.LENGTH_SHORT).show();
        //3
        initRecyclerView();
        //实例化model 然后调用方法  获取网络数据
        //4实例化
        // ArticleModel articleModel = new ArticleModel();
        TcaseModel tcaseModel = new TcaseModel();
        //articleModel.getResultList("tcase",2,"k17vmm42ui9al6clh0lo3de2t7",articleListener);
        tcaseModel.getResultList("project", 2, "k17vmm42ui9al6clh0lo3de2t7", articleListener);


        //返回动态生成的view
        return view;
    }
}
